#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/4/2 16:36
# @Author  : Chenmm
# @File    : start.py
# @Software: PyCharm

import os
from app import create_app, db
from flask import request, render_template, jsonify

app = create_app(os.getenv('FLASK_CONFIG') or 'default')


from app.models import SysUser

@app.shell_context_processor
def make_shell_context():
    return dict(db=db, SysUser=SysUser)


# @app.errorhandler(404)
# def page_not_found(e):
#     return render_template('errors/404.html'), 404

@app.before_request
def before():
    # print("请求地址：" + str(request.path))
    # print("请求方法：" + str(request.method))
    # print("---请求headers--start--")
    # print(str(request.headers).rstrip())
    # print("---请求headers--end----")
    # print("GET参数：" + str(request.args))
    # print("POST参数：" + str(request.form))

    url = request.path  # 当前请求的URL
    passUrl = ["/login"]
    if url in passUrl:
        pass
    # else:
    #     return jsonify({'msg': '当前操作没有权限', 'code': 403})
    #     _id = session.get("_id", None)
    #     if not _id:
    #         return jsonify(401, {"msg": "认证失败，无法访问系统资源"})
    #     else:
    #         pass


from flask import jsonify

@app.errorhandler(401)
def unauthorized(e):
    response = jsonify({'msg': '尚未登录，没有权限', 'code': 401})
    response.status_code = 401
    return response

@app.errorhandler(403)
def forbidden(e):
    response = jsonify({'msg': 'forbidden', 'code': 403})
    response.status_code = 403
    return response


@app.errorhandler(500)
def internal_server_error(e):
    response = jsonify({'msg': 'internal server error', 'code': 500})
    response.status_code = 500
    return response



# @app.errorhandler(400)
# def bad_request(e):
#     response = jsonify({'msg': 'bad request', 'code': 400})
#     response.status_code = 400
#     return response


if __name__ == '__main__':
    # for rule in app.url_map.iter_rules():
    #     print(rule)
    app.run(debug=True, port=5000)